﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using JsonServer.FYPDataSetTableAdapters;


namespace JsonServer
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "FypService" in code, svc and config file together.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class FypService : IFypService
    {

        public Power GetOnePower(string id)
        {
            // lookup person with the requested id 
            FYPPowerTableTableAdapter adap = new FYPPowerTableTableAdapter();
            var powerTable = adap.GetDataById(int.Parse(id));

            return new Power()
            {
                id = Convert.ToInt32(id),
                power = Convert.ToString(powerTable.First().Power),
                createtime = "  " + powerTable.First().CreateDate
            };
        }

        public void InsertOneSecondPower(Power eachPower)
        {
            FYPPowerTableTableAdapter adap = new FYPPowerTableTableAdapter();
            var insert = adap.InsertPower(eachPower.power, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
        }

        public void InsertPower(Power[] powerIn)
        {
            FYPPowerTableTableAdapter adap = new FYPPowerTableTableAdapter();

            foreach (var power in powerIn)
            {
                var insert = adap.InsertPower(power.power, power.createtime);
            }

       
        }

        public string GetOneDayPower(string year, string month, string day)
        {
            // lookup person with the requested id 
            FYPPowerTableTableAdapter adap = new FYPPowerTableTableAdapter();
            var powerTable = adap.GetDataByDay(year, month, day);
            double DayPower = 0;

            foreach (var item in powerTable)
            {
                double eachPower = Convert.ToDouble(item.Power);
                DayPower += eachPower;
            }
            string sDayPower = DayPower.ToString("#.###0");
            return sDayPower;
        }

        public string GetOneMonthPower(string year, string month)
        {
            // lookup person with the requested id 
            FYPPowerTableTableAdapter adap = new FYPPowerTableTableAdapter();
            var powerTable = adap.GetDataByMonth(year, month);
            double MonthPower = 0;

            foreach (var item in powerTable)
            {
                double eachPower = Convert.ToDouble(item.Power);
                MonthPower += eachPower;
            }
            string sMonthPower = MonthPower.ToString("#.###0");
            return sMonthPower;
        }


    


        public string GetAllPower()
        {
            double totalPower = 0;
            FYPPowerTableTableAdapter adap = new FYPPowerTableTableAdapter();
            var powerTable = adap.GetData();
            foreach (var item in powerTable)
            {
                double eachPower = Convert.ToDouble(item.Power);
                totalPower += eachPower;
            }
            return totalPower.ToString("#.###0");
        }
    }
}
